"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const log_1 = require("ee-core/log");
const ps_1 = require("ee-core/ps");
const message_1 = require("ee-core/message");
const hello_1 = require("./hello");
const user_1 = require("../../service/job/user");
/**
 * example - TimerJob
 * @class
 */
class TimerJob {
    constructor(params) {
        Object.defineProperty(this, "params", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "timer", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "timeoutTimer", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "number", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "countdown", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.params = params;
        this.timer = undefined;
        this.timeoutTimer = undefined;
        this.number = 0;
        this.countdown = 10; // 倒计时
    }
    /**
     * handle() method is necessary and will be automatically called
     */
    handle() {
        return __awaiter(this, void 0, void 0, function* () {
            log_1.logger.info("[child-process] TimerJob params: ", this.params);
            const { jobId } = this.params;
            // Use service in child process
            // 1. Ensure that the service does not have Electron's API or dependencies, as Electron does not support them
            const userService = new user_1.UserService();
            userService.hello('job');
            // Execute the task
            this.doTimer(jobId);
        });
    }
    /**
     * Pause the job
     */
    pause(jobId) {
        return __awaiter(this, void 0, void 0, function* () {
            log_1.logger.info("[child-process] Pause timerJob, jobId: ", jobId);
            clearInterval(this.timer);
            clearInterval(this.timeoutTimer);
        });
    }
    /**
     * Resume the job
     */
    resume(jobId, pid) {
        return __awaiter(this, void 0, void 0, function* () {
            log_1.logger.info("[child-process] Resume timerJob, jobId: ", jobId, ", pid: ", pid);
            this.doTimer(jobId);
        });
    }
    /**
     * Run the task
     */
    doTimer(jobId) {
        return __awaiter(this, void 0, void 0, function* () {
            //  Timer to simulate the task
            const eventName = 'job-timer-progress-' + jobId;
            this.timer = setInterval(() => {
                (0, hello_1.welcome)();
                message_1.childMessage.send(eventName, { jobId, number: this.number, end: false });
                this.number++;
                this.countdown--;
            }, 1000);
            // Use setTimeout to simulate the task duration
            this.timeoutTimer = setTimeout(() => {
                // Stop the timer to simulate the task
                clearInterval(this.timer);
                // Task completed, reset the front-end display
                message_1.childMessage.send(eventName, { jobId, number: 0, pid: 0, end: true });
                // If it is a childJob task, call exit() to exit the process, otherwise it will stay in memory
                // If it is a childPoolJob task, stay in memory and wait for the next business
                if ((0, ps_1.isChildJob)()) {
                    (0, ps_1.exit)();
                }
            }, this.countdown * 1000);
        });
    }
}
TimerJob.toString = () => '[class TimerJob]';
exports.default = TimerJob;
